package com.epfresh.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.adapter.CommonAdapter;
import com.epfresh.api.adapter.ViewHolder;
import com.epfresh.api.entity.*;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.FormatUtil;
import com.epfresh.bean.BookDetailMonth;
import com.epfresh.global.BaseActivity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class BookDetailWeekActivity extends BaseActivity {


    private TextView tv_time_now,tv_money_now;//,tvToday,tvTime,tvDealTime;
    View tvMoreBest,tvMoreDetail,ivTime;
    ListView lvDetail;
    DetailAdapter detailAdapter;

    ArrayList<BookDetailMonth.ListBean> detailList = new ArrayList<>();

    //String timeTitle,timeValue,money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_detail);
/*        timeTitle = getIntent().getStringExtra("timeTitle");
        timeValue = getIntent().getStringExtra("timeValue");
        money = getIntent().getStringExtra("money");*/
        ivBack.setVisibility(View.VISIBLE);
        toolbarTitle.setText("每周明细");
        lvDetail = (ListView)findViewById(R.id.lv_detail);
        detailAdapter = new DetailAdapter(this,detailList,R.layout.item_book_detail);
        lvDetail.setAdapter(detailAdapter);


/*        if(getIntent().getSerializableExtra("goodsList")!=null){
            detailList.addAll((List<BookDetailMonth.DetailListBean>)getIntent().getSerializableExtra("goodsList"));
            if(detailList==null||detailList.size()<=0){
                T.toast("没收到id");
                finish();
            }else {
                detailAdapter.notifyDataSetChanged();
            }
        }*/

        mHttpLoad();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
        }
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    class DetailAdapter extends CommonAdapter<BookDetailMonth.ListBean> implements View.OnClickListener {
        public DetailAdapter(Context context, ArrayList<BookDetailMonth.ListBean> runPacks, int a) {
            super(context, runPacks, a);
        }

        public void onClick(View v) {
            Object tag = v.getTag(R.id.item_key_position);
            if (tag != null && tag instanceof Integer) {
                int position = (int) tag;
                    Intent detailIntent = new Intent(mContext,
                            BookDetailDayActivity.class);
                    detailIntent.putExtra("timeTitle", detailList.get(position).getStrDate());
                    detailIntent.putExtra("timeValue", detailList.get(position).getQueryDate());
                    detailIntent.putExtra("money", detailList.get(position).getPrice());
                    startActivity(detailIntent);
            }
        }

        @Override
        public int getCount() {
            if (mDatas == null) {
                return 0;
            }
            return mDatas.size();
        }

        @Override
        public void convert(final ViewHolder holder, final BookDetailMonth.ListBean bean) {
            holder.getView(R.id.ll_item).setTag(R.id.item_key_position, detailList.indexOf(bean));
            //holder.setOnClickListener(R.id.ll_item, this);
            holder.setText(R.id.tv_time, bean.getStrDate());
            holder.setText(R.id.tv_money, FormatUtil.formatDecimal(bean.getPrice())+"元");
            holder.getView(R.id.iv_right).setVisibility(View.GONE);
        }
    }

    /**
     * 加载收入数据
     */
    private void mHttpLoad() {
        RequestEntityPurchase<Map<String, String>> data = new RequestEntityPurchase<>();
        data.setCmd(com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_order_income_incomeWeekConsumeForYear);
        Map<String, String> map = new HashMap<>();
        //map.put("queryDate", timeValue);
        map.put("accountId",getUser().getAccountId());
        data.setParameters(map);
        request(data, com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_order_income_incomeWeekConsumeForYear, onRequestListener);
    }
    BookDetailMonth data;
    private OnRequestListener<BookDetailMonth> onRequestListener = new OnRequestListener<BookDetailMonth>() {
        @Override
        public BookDetailMonth jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, BookDetailMonth.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<BookDetailMonth> response, Object tag) {

            hideProgressDialog();
            BookDetailMonth responseElement = response.getResponseElement();
            data = responseElement;
            detailList.clear();
            detailList.addAll(responseElement.getDataList());
            detailAdapter.notifyDataSetChanged();

        }
    };
}
